package Four;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 长风无言
 */
public class StudentFunctionImplements implements AddStudents,DeleteStudents,FindStudents,ModifyStudents,ForStudents {

    private List<Student> students = new ArrayList<>();

    @Override
    public void addStudent(String name, int num, int age) {
        //添加学生信息
        students.add(new Student(name,num,age));
        System.out.println("添加成功");
    }

    @Override
    public boolean deleteStudents(int num) {
        Iterator<Student> iterator = students.iterator();
        // 判断是否还有迭代对象
        while(iterator.hasNext()){
            // 获取学生对象
            Student student = iterator.next();
            // 判断学生对象的学号和输入的学号是否相同
            if(student.getNum() == num){
                iterator.remove();
                System.out.println("删除成功");
                return true;
            }
        }
        System.out.println("删除失败，查无此人");
        return false;
    }

    @Override
    public void findStudents(int num) {
        for(Student student:students){
            // 判断学生对象中的姓名和查找的姓名是否相同
            if(num == student.getNum()){
                System.out.println(student.toString());
            }
        }
        System.out.println("查无此人");
    }

    @Override
    public Student modifyStudents(int num,String name, int newnum, int age) {
        for(Student student:students){
            // 判断学生对象的学号是否为要修改信息的学生的雪蛤
            if(student.getNum() == num){
                student.setNum(newnum);
                student.setName(name);
                student.setAge(age);
                System.out.println("修改成功");
                return student;
            }
        }
        // 修改失败，无此学生
        System.out.println("修改失败，查无此人");
        return null;
    }

    @Override
    public void forStudent() {
        //遍历每一个学生信息
        for(Student student:students){
            //打印该学生信息
            System.out.println(student.toString());
        }
    }
}
